/**
 * Created by RickyTT on 14-1-25.
 */
var db1Obj = require('./db');
var User = require('./user.js');
var UserInfo = require('./userInfo.js');
var async = require('async');
var t = require('../lib/t.js');

function UserCollection(user,userInfo){
    this.user = user;
    this.userInfo = userInfo;
};

module.exports = UserCollection;

UserCollection.getById = function getById(id,callback){
    var userModel = null;
    var userInfoModel = null;

    /*User.getById(id,function(err,result){
        if(err){
            console.log(err);
        }else{
            userModel = result;
            console.log('user data'+result);
            UserInfo.getByUserId(id,function(err,result){
                if(err){
                    console.log(err);
                }else{
                    userInfoModel = result;
                    console.log('userInfo data'+result);
                    var userCollection = new UserCollection(userModel,userInfoModel);
                    callback(1,userCollection);
                };
            });
        };
    });*/

    //异步串发执行
    /*var sqls = {
        'selectUser':'select * from user where id = '+id,
        'selectUserInfo':'select * from userInfo where userId = '+id
    };

    var tasks = ['selectUser','selectUserInfo'];

    async.eachSeries(tasks,function(item,callback1){
        console.log('item name is ======>'+item);
        console.log('item is ======>'+sqls[item]);
        db1Obj.sql(sqls[item],function(err,result){
            if(item === 'selectUser'){
                userModel = new User(result[0]);
                //console.log('user data'+result[0]);
            }
            else{
                userInfoModel = new UserInfo(result[0]);
                //console.log('userInfo data'+result[0]);
            };
            callback1(err,result[0]);
        })
    },function(err){
        var userCollection = new UserCollection(userModel,userInfoModel);
        callback(1,userCollection);
    });*/


    //异步并发执行
    async.parallel([
        function(cb){
            User.getById(id,function(err,result){
                //t.fire(result,cb,1);
                cb(null,result);
            });
        },
        function(cb){
            UserInfo.getByUserId(id,function(err,result){
//                t.fire(result,cb,1);
                cb(null,result);
            });
        }
    ],function(err,result){
//        console.log(result);
        var userCollection = new UserCollection(result[0],result[1]);
        callback(err,userCollection);
    });
};

